package com.chat.view;

import com.chat.service.GetGroupVerify;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.io.IOException;

public class GroupVerifyController {
    public ListView verify;
    public Label blanktishi;
    public static ObservableList<VerifyData> groupver= FXCollections.observableArrayList();
    public void tuichu(){
        Stage stage=(Stage) blanktishi.getScene().getWindow();
        stage.close();
        try {
            GroupMsgView.start(new Stage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void initialize(){
        verify.getItems().clear();
        groupver.clear();

        GetGroupVerify ggv=new GetGroupVerify();
        ggv.getgroupverify();

        verify.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

        verify.setItems(groupver);

        verify.setCellFactory(new Callback<ListView, ListCell>() {
            @Override
            public ListCell call(ListView param) {
                return new ListCell<VerifyData>(){
                    @Override
                    protected void updateItem(VerifyData item, boolean empty){
                        super.updateItem(item,empty);
                        if(!empty){
//                                setStyle("-fx-background-color:transparent");

                            HBox hbox=new HBox();
                            FXMLLoader fxmlLoader=new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("verifylist.fxml"));
                            try {
                                hbox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            setOnMouseClicked(event ->{//设置每个list的点击事件
                            });
//                                hbox.setPrefHeight(30);
                            VerifyListController verifyListController=fxmlLoader.getController();
                            hbox.setPadding(new Insets(20,0,0,0));
                            hbox.setPrefHeight(50);
                            verifyListController.setId(item.getId());//放置用户id
                            verifyListController.setNickname(item.getNickname());//放置用户昵称
                            verifyListController.setState(item.getState());
                            Platform.runLater(()-> {
                                this.setGraphic(hbox);
                            });

                        }
                        else {
                            Platform.runLater(()-> {
                                this.setGraphic(null);
                            });

                        }
                    }
                };
            }
        });
    }
}
